<template>
    <div>
        <!-- 导航栏 -->
        <div>
            <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item>用户管理></el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <!-- 表格 -->
        <div>
            <el-card class="box-card">
                <div>
                    <div style="float:left">
                        <el-button @click="addUser()" type="success">添加用户</el-button>
                        <el-select v-model="value" style="width:135px" placeholder="请选择">
                            <el-option
                            v-for="item in options"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                            </el-option>
                        </el-select>
                        <el-input v-model="value2" style="width:250px"></el-input>
                        <el-button @click="tiaojianchaxun()" type="success">查询</el-button>
                        <el-button type="success" @click="chongzhi()">重置</el-button>
                    </div>
                <el-table
                :data="tableData"
                height="500px"
                stripe
                style="width: 100%">
                <el-table-column
                type="index"
                >
                </el-table-column>
                <el-table-column
                prop="username"
                label="用户名">
                </el-table-column>
                <el-table-column
                prop="authority"
                label="权限">
                <template slot-scope="scope">
                    <el-tag v-if="scope.row.authority==0">管理员</el-tag>
                    <el-tag v-else>学生</el-tag>
                </template>
                </el-table-column>
                <el-table-column
                prop="telephone"
                show-overflow-tooltip
                label="手机号">
                </el-table-column>
                <el-table-column
                prop="studentId"
                show-overflow-tooltip
                label="学号">
                </el-table-column>
                <el-table-column
                prop="idCard"
                show-overflow-tooltip
                label="身份证号">
                </el-table-column>
                <el-table-column
                prop="email"
                show-overflow-tooltip
                label="邮箱">
                </el-table-column>
                <el-table-column
                prop="address"
                label="操作">
                <template slot-scope="scope">
                    <el-button @click="editData(scope.row)" type="text" size="small">编辑</el-button>
                    <el-button @click="deleteData(scope.row)" type="text" size="small">删除</el-button>
                </template>
                </el-table-column>
            </el-table>
            </div>
            <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="count"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
            </el-pagination>
            </el-card>
            <el-dialog
            title="编辑"
            :visible.sync="centerDialogVisible"
            width="45%"
            height="450px"
            center>
            <el-form ref="form" :model="form" label-width="80px">
                <el-form-item label="学号">
                    <el-input v-model="form.studentId" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="用户名">
                    <el-input v-model="form.username" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="邮箱">
                    <el-input v-model="form.email" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="联系方式">
                    <el-input v-model="form.telephone" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="身份证号">
                    <el-input v-model="form.idCard" :disabled="true"></el-input>
                </el-form-item>
                <!-- <el-form-item label="号">
                    <el-input v-model="form.notificationNumber" :disabled="true"></el-input>
                </el-form-item> -->
                <el-form-item label="手机号">
                    <el-input v-model="form.telephone" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="头像">
                    <el-input v-model="form.avatar" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="反馈时间">
                    <el-input v-model="form.feedbackTime" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="权限">
                    <el-select v-model="form.authority" placeholder="请选择">
                    <el-option label="学生" value="1"></el-option>
                    <el-option label="管理员" value="0"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="centerDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="commitData()">提交</el-button>
            </span>
            </el-dialog>
            <el-dialog
            title="添加用户"
            :visible.sync="centerDialogVisible2"
            width="45%"
            height="450px"
            center>
            <el-form ref="form2" :model="form2" label-width="80px">
                <el-form-item label="学号">
                    <el-input v-model="form.studentId"></el-input>
                </el-form-item>
                <el-form-item label="用户名">
                    <el-input v-model="form.username"></el-input>
                </el-form-item>
                <el-form-item label="密码">
                    <el-input v-model="form.password"></el-input>
                </el-form-item>
                <el-form-item label="邮箱">
                    <el-input v-model="form.email"></el-input>
                </el-form-item>
                <el-form-item label="联系方式">
                    <el-input v-model="form.telephone"></el-input>
                </el-form-item>
                <el-form-item label="身份证号">
                    <el-input v-model="form.idCard"></el-input>
                </el-form-item>
                <el-form-item label="权限">
                    <el-select v-model="form.authority" placeholder="请选择">
                    <el-option label="学生" value="1"></el-option>
                    <el-option label="管理员" value="0"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="centerDialogVisible2 = false">取 消</el-button>
                <el-button type="primary" @click="commitData2()">提交</el-button>
            </span>
            </el-dialog>
        </div>
    </div>
</template>
<script>
export default {
    data(){
        return{
            tableData: [],
            total:10,
            page:1,
            count:10,
            centerDialogVisible:false,
            centerDialogVisible2:false,
            form:{},
            form2:{
                "studentId":'',
                "username":'',
                "email":'',
                "telephone":'',
                "authority":'',
                "password":''
            },
            options:[
                {value:'0',label:'用户名'},
                {value:'1',label:'学号'},
                {value:'2',label:'身份证号'},
            ],
            value:'',
            value2:''
        }
    },
    created(){
        this.getUserList()
    },
    methods:{
        getUserList(){
            var _this=this
            var url='http://taruinfo.work:8088/getByPage?current='+_this.page+'&size='+_this.count
            _this.$axios.get(url).then(function(res){
                console.log(res.data)
                if(res.data !=''){
                  _this.total=res.data.data.total
                  _this.tableData=res.data.data.records
                }else{
                    console.log('获取所有用户信息失败')
                }
            })
        },
        handleSizeChange(val){
            this.count=val
            this.getUserList()
        },
        handleCurrentChange(val){
            this.page=val
            this.getUserList()
        },
        editData(row) {
            console.log(row)
            this.form=row
            this.centerDialogVisible=true
        },
        addUser(){
            this.centerDialogVisible2=true
        },
        commitData2(){
            var _this=this
            var url='http://taruinfo.work:8088/insertUser'
            _this.$axios.post(url,_this.form).then(function(res){
                if(res.data.code==200){
                    _this.$message({
                      message: '添加成功',
                      type: 'success'
                    })
                    _this.centerDialogVisible2=false
                    _this.getUserList()
                }else{
                   _this.$message({
                      message: '添加失败',
                      type: 'error'
                    })
                   _this.centerDialogVisible2=false 
                }
            })
        },
        commitData(){
            var _this=this
            var url='http://taruinfo.work:8088/reg/update1'
            _this.$axios.put(url,_this.form).then(function(res){
                if(res.data.code==200){
                    _this.$message({
                      message: '更新成功',
                      type: 'success'
                    })
                    _this.centerDialogVisible=false
                    _this.getUserList()
                }else{
                   _this.$message({
                      message: '更新失败',
                      type: 'error'
                    })
                   _this.centerDialogVisible=false 
                }
            })
        },
        deleteData(row){
            var _this=this
            var url='http://taruinfo.work:8088/delById?id='+row.id
            _this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
            }).then(() => {
                _this.$axios.delete(url).then(function(res){
                if(res.data.code==200){
                    _this.$message({
                        type: 'success',
                        message: '删除成功!'
                    })
                    _this.getUserList()
                }else{
                    _this.$message({
                        type: 'error',
                        message: '删除失败!'
                    })
                }
            })  
            }).catch(() => {
                _this.$message({
                    type: 'info',
                    message: '已取消删除'
                })         
            })
        console.log(row)
        },
        chongzhi(){
            this.getUserList()
        },
        getphotoAdd(scope){
            return scope.avatar
        },
        tiaojianchaxun(){
            var _this=this
            if(_this.value2!=''){
                if(_this.value == '2'){
                var url='http://taruinfo.work:8088/likeByUserList?current=1&idCard='+_this.value2+'&size=10'
                _this.$axios.get(url).then(function(res){
                    console.log('条件查询')
                    console.log(res.data)
                    if(res.data.code=='200'){
                    _this.total=res.data.data.total
                    _this.tableData=res.data.data.records
                    _this.value=''
                    _this.value2=''
                    }else{
                        console.log('获取所有用户信息失败')
                    }
                })
            }else if(_this.value == '0'){
                var url='http://taruinfo.work:8088/likeByUserList?current=1&username='+_this.value2+'&size=10'
                _this.$axios.get(url).then(function(res){
                    // console.log(res.data)
                    if(res.data.code=='200'){
                    _this.total=res.data.data.total
                    _this.tableData=res.data.data.records
                    _this.value=''
                    _this.value2=''
                    }else{
                        console.log('获取所有用户信息失败')
                    }
                })
            }else if(_this.value == '1'){
                var url='http://taruinfo.work:8088/likeByUserList?current=1&size=10&studentId='+_this.value2
                _this.$axios.get(url).then(function(res){
                    // console.log(res.data)
                    if(res.data.code=='200'){
                    _this.total=res.data.data.total
                    _this.tableData=res.data.data.records
                    _this.value=''
                    _this.value2=''
                    }else{
                        console.log('获取所有用户信息失败')
                    }
                })
            }else{

            }
            }else{
                _this.$message({
                    message: '条件不能为空',
                    type: 'warning'
                })
            }
        }
    }
}
</script>

<style scoped>
.box-card{
    width: 1270px;
    height:615px;
    margin-top: 10px;
}
</style>